package com.maxh.controller;


import com.alibaba.fastjson.JSONObject;
import com.maxh.annotation.PassToken;
import com.maxh.annotation.SuperAuthority;
import com.maxh.annotation.AdminLoginToken;
import com.maxh.pojo.Attribute;
import com.maxh.service.AttributeService;
import com.maxh.service.CourseService;
import com.maxh.service.TokenService;
import com.maxh.util.ReturnData;
import com.maxh.util.ReturnError;
import com.maxh.util.ReturnUtil;
import com.maxh.util.StatusCode;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@RestController
public class AttributeController {
    @Autowired
    AttributeService attributeService;
    @Autowired
    TokenService tokenService;
    @Autowired
    CourseService courseService;

    /*管理员获取已启用的课程属性信息*/
    @PassToken
    @GetMapping("/training/course/attribute/list")
    public Object attributeList(){
        List attributeList = (List) attributeService.attributeList();
        System.out.println("管理员获取已启用的课程属性信息："+attributeList);
        if(attributeList.size()>0){
            return new ReturnData(StatusCode.REQUEST_SUCCESS,attributeList ,"数据获取完成！");
        }else{
            return new ReturnData(StatusCode.REQUEST_SUCCESS,attributeList,"数据为空");
        }
    }

    /*管理员获取所有的课程属性信息*/
    @AdminLoginToken
    @GetMapping("/training/course/attribute/listAll")
    public Object attributeListAll(){
        List attributeList = (List) attributeService.attributeListAll();
        System.out.println("管理员获取所有的课程属性信息："+attributeList);
        if(attributeList.size()>0){
            return new ReturnData(StatusCode.REQUEST_SUCCESS,attributeList ,"数据获取完成！");
        }else{
            return new ReturnData(StatusCode.REQUEST_SUCCESS,attributeList,"数据为空");
        }
    }

    /*管理员添加课程分类属性*/
    @AdminLoginToken
    @PostMapping("/training/course/attribute/add")
    public Object addAttribute(@RequestParam Map<String,Object> map){
        System.out.println("管理员添加课程分类属性："+map);
        if(map.get("attributeName")==null){
            return new ReturnError(StatusCode.RETURN_ERROR,"分类名不能为空");
        }
        /*当前时间*/
        map.put("createTime",new ReturnUtil().nowTime());
        map.put("useStatus",1);

        Attribute attributeList = attributeService.searchByName(map.get("attributeName")+"");
        if(attributeList!=null) return new ReturnError(StatusCode.RETURN_ERROR,"分类名已存在！");
        Boolean isAdd = attributeService.addAttribute(map);
        System.out.println(isAdd);
        if(isAdd){
            //根据id获取课程属性
            Attribute attribute = attributeService.searchByName(map.get("attributeName")+"");
            return new ReturnData(StatusCode.REQUEST_SUCCESS, attribute,"添加成功！");
        }else{
            return new ReturnError(StatusCode.RETURN_ERROR,"添加分类失败");
        }
    }

    /*管理员修改课程分类属性*/
    @SuperAuthority
    @PostMapping("/training/course/attribute/update")
    public Object updateAttribute(@RequestParam Map<String,Object> map){
        System.out.println("管理员修改课程分类属性："+map);
        if(map.get("attributeName")==null){
            return new ReturnError(StatusCode.RETURN_ERROR,"分类名不能为空");
        }
        //判断分类是否存在
        Attribute attributeList = attributeService.searchByName(map.get("attributeName")+"");
        if(attributeList!=null) return new ReturnError(StatusCode.RETURN_ERROR,"分类名已存在！");

        Boolean isAdd = attributeService.updateAttribute(map);
        System.out.println(isAdd);
        if(isAdd){
            return new ReturnData(StatusCode.REQUEST_SUCCESS, map,"修改成功！");
        }else{
            return new ReturnError(StatusCode.RETURN_ERROR,"修改失败");
        }
    }
    /*管理员修改课程属性使用状态*/
    @AdminLoginToken
    @PostMapping("/training/course/attribute/updateStatus")
    public Object updateAttributeStatus(@RequestParam Map<String,Object> map){
        System.out.println("管理员修改课程属性使用状态："+map);
        if(map.get("id")==null || map.get("useStatus")==null){
            return new ReturnError(StatusCode.RETURN_ERROR,"必填数据不能为空");
        }
        Boolean isAdd = attributeService.updateAttribute(map);
        if(isAdd){
            return new ReturnData(StatusCode.REQUEST_SUCCESS, map,"修改成功！");
        }else{
            return new ReturnError(StatusCode.RETURN_ERROR,"修改失败");
        }
    }

    /*学员获取课程属性信息和课程信息*/
    @PassToken
    @GetMapping("/training/course/attributeCourse/list")
    public Object attributeCourseList(){
        List attributeList = (List)attributeService.attributeList();
        List courseList = courseService.curriculumList();
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("attributeList",attributeList);
        jsonObject.put("courseList",courseList);
        System.out.println("学员获取课程属性信息和课程信息"+jsonObject);
        if(attributeList.size()>0){
            return new ReturnData(StatusCode.REQUEST_SUCCESS,jsonObject,"数据获取完成！");
        }else{
            return new ReturnData(StatusCode.REQUEST_SUCCESS,jsonObject,"数据为空");
        }
    }
}
